package com.source.baseData.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.baseData.vo.EntrustDefaultInfoVO;
import com.source.baseData.vo.EntrustVO;
import com.source.baseData.entity.Entrust;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author Feng
 * @since 2021-02-12
 */
public interface EntrustMapper extends BaseMapper<Entrust> {

    String SELECT = "SELECT e.*,u.`name` userName,t.slaughterHouseName toMarketName,t.stallName toMarketStallName,s.stallsName,s.slaughterHouseId,sh.slaughterHouseName,p.poultryCategoryName\n" +
            "FROM entrust e,sys_user u,to_market t,stalls s,slaughter_house sh,poultry_category p\n" +
            "WHERE e.isDelete=0 AND u.userId=e.userId AND e.toMarketId=t.slaughterHouseId AND s.stallsId=e.stallsId AND s.slaughterHouseId=sh.slaughterHouseId AND p.poultryCategoryId=e.poultryId ";

    /*委托人姓名	去向市场	屠宰市场	禽类	委托数量	状态*/
    String LIKE = " CONCAT(IFNULL(u.`name`,''),'\\t',IFNULL(t.slaughterHouseName,''),'\\t',IFNULL(sh.slaughterHouseName,''),'\\t',IFNULL(poultryCategoryName,''),'\\t',IFNULL(poultryCount,''),'\\t', (CASE e.status WHEN 0 THEN '已删除委托' WHEN 1 THEN '等待中委托' WHEN 2 THEN '已接单委托' WHEN 3 THEN '已拒单委托' ELSE '' END)) LIKE CONCAT('%',#{word},'%')";

    int min = 60;// 设置“正在贴标”的委托贴的正在贴标时间与当前时间差距多少分钟时，该委托被视为“已完成”

    @Select(SELECT)
    List<EntrustVO> listAll();

    @Select(SELECT)
    Page<EntrustVO> pageAll(Page<EntrustVO> page);

    @Select(SELECT + "AND e.entrustId=#{entrustId}")
    EntrustVO getByEntrustId(@Param("entrustId") String id);

    @Select(SELECT + "AND e.toMarketId=#{toMarketId}")
    List<EntrustVO> listByToMarketId(@Param("toMarketId") String toMarketId);

    @Select(SELECT + "AND e.toMarketId=#{toMarketId}")
    Page<EntrustVO> pageByToMarketId(@Param("toMarketId") String toMarketId, Page<EntrustVO> page);

    @Select(SELECT + "AND e.toMarketId=#{toMarketId} AND e.status=#{status}")
    List<EntrustVO> listByToMarketIdAndStatus(@Param("toMarketId") String toMarketId, @Param("status") int status);

    @Select(SELECT + "AND e.toMarketId=#{toMarketId} AND e.status=#{status}")
    Page<EntrustVO> pageByToMarketIdAndStatus(@Param("toMarketId") String toMarketId, @Param("status") int status, Page<EntrustVO> page);

    @Select(SELECT + "AND e.userId=#{userId}")
    List<EntrustVO> listByUserId(@Param("userId") String userId);

    @Select(SELECT + "AND e.userId=#{userId}")
    Page<EntrustVO> pageByUserId(@Param("userId") String userId, Page<EntrustVO> page);

    @Select(SELECT + "AND e.userId=#{userId} AND e.status=#{status}")
    List<EntrustVO> listByUserIdAndStatus(@Param("userId") String userId, @Param("status") int status);

    @Select(SELECT + "AND e.userId=#{userId} AND e.status=#{status}")
    Page<EntrustVO> pageByUserIdAndStatus(@Param("userId") String userId, @Param("status") int status, Page<EntrustVO> page);

    @Select(SELECT + "AND" + LIKE)
    List<EntrustVO> listLike(@Param("word") String word);

    @Select(SELECT + "AND" + LIKE)
    Page<EntrustVO> pageLike(@Param("word") String word, Page<EntrustVO> page);

    @Update("UPDATE entrust SET `status` = 6 WHERE `status` NOT IN (3,4,5,6) AND goMarketTime < CurDate()")
    void invalidate();

    @Select(value = "SELECT * FROM entrust WHERE userId = #{userId} ORDER BY createDate DESC LIMIT 1")
    Entrust getLastEntrustByUserId(String userId);

    //EntrustDefaultInfoVO defaultInfo(String userId);

    @Select("SELECT * FROM entrust e where `status`!=0 AND userId = #{userId} AND goMarketTime = CurDate() ")
    IPage<Entrust> findGoMarketTodayByUserId(String userId, Page<Entrust> page);

    @Select("SELECT * FROM entrust e where userId = (?1) AND `status` = (?2) AND goMarketTime = CurDate() ")
    IPage<Entrust> findGoMarketTodayByUserId(String userId, Integer status, Page<Entrust> page);

    @Select("SELECT * FROM entrust e where `status`!=0 AND toMarketId = (?1) AND goMarketTime = CurDate()")
    IPage<Entrust> findGoMarketTodayByToMarket(String toMarketId, Page<Entrust> page);

    @Select("SELECT * FROM entrust e where `status` = #{status} AND toMarketId = #{toMarketId} AND goMarketTime = CurDate()  ")
    IPage<Entrust> findGoMarketTodayByToMarket(String toMarketId, Integer status, Page<Entrust> page);

    @Select("SELECT * FROM entrust e where `status`!=0 AND stallsId = (?1) AND goMarketTime = CurDate()")
    IPage<Entrust> findGoMarketTodayByStalls(String stallsId, Page<Entrust> page);

    @Select("SELECT * FROM entrust e where `status` = #{status} AND stallsId = #{stallsId} AND goMarketTime = CurDate()")
    IPage<Entrust> findGoMarketTodayByStalls(String stallsId, Integer status, Page<Entrust> page);

    @Update("UPDATE entrust SET `status`= #{presentState} WHERE machineNumber= #{barcodeMachineNumber} AND `status`=#{originalState}")
    void updateStateByBarcodeNumberAndState(String barcodeMachineNumber, int originalState, int presentState);

    String SELECT_FINISHED_ENTRUST = "SELECT * FROM entrust WHERE (`status`=5 OR `status`=4 AND TIMESTAMPDIFF(MINUTE,labellingTime,NOW()) > " + min + ") ";

    @Select(SELECT_FINISHED_ENTRUST + " AND userId= #{userId} AND createDate BETWEEN #{from} AND #{to} ")
    IPage<Entrust> historyByUser(String userId, Page<Entrust> page, Date from, Date to);

    @Select(SELECT_FINISHED_ENTRUST + "  AND userId= #{userId} AND createDate BETWEEN #{from} AND #{to} AND status = {status}")
    IPage<Entrust> historyByUserAndStatus(String userId, Integer status, Page<Entrust> page, Date from, Date to);

    @Select(SELECT_FINISHED_ENTRUST + " AND toMarketId= #{toMarketId} AND createDate BETWEEN #{from} AND #{to} ")
    IPage<Entrust> historyByToMarket(String toMarketId, Page<Entrust> page, Date from, Date to);

    @Select(SELECT_FINISHED_ENTRUST + " AND toMarketId= #{toMarketId} AND createDate BETWEEN #{from} AND #{to} AND status = #{status} ")
    IPage<Entrust> historyByToMarketAndState(String toMarketId, Integer status, Page<Entrust> page, Date from, Date to);

    @Select(SELECT_FINISHED_ENTRUST + " AND stallsId=#{stallsId} AND createDate BETWEEN #{from} AND #{to} ")
    IPage<Entrust> historyByStalls(String stallsId, Page<Entrust> page, Date from, Date to);

    @Select(SELECT_FINISHED_ENTRUST + " AND stallsId=#{stallsId} AND createDate BETWEEN #{from} AND #{to} AND status = #{status} ")
    IPage<Entrust> historyByStallsAndState(String stallsId, Integer status, Page<Entrust> page, Date from, Date to);


}
